package com.nbpt.edu.commercesites.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nbpt.edu.commercesites.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pojo.Position;
import pojo.Result;
import pojo.Staff;
import pojo.entity.StaffEntity;
import pojo.vo.PageResult;
import pojo.vo.StaffVo;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    StaffService staffService;
    @PostMapping("/login")
    Result<Boolean> login(@RequestParam("uUsername") String uName,@RequestParam("uPassword") String uPassword){
        Boolean login = staffService.login(uName, uPassword);
        if(login ==true){
            return Result.successLogin(login);
        }
        return Result.ErrorLogin(login);
    }
    @PostMapping("/register")
    public Result<Boolean> register(@RequestBody Staff user){
        Boolean register = staffService.register(user);
        if(register ==true){
            return Result.successRegister(true);
        }
        return Result.ErrorRegister(false);
    }
    @PostMapping("/getposition")
    Result<Position> getPosition(String u_username){
        return Result.success(staffService.getPosition(u_username));
    }
    @PostMapping("/getUser")
    Result<Staff> getUser(String u_username){
        return Result.success(staffService.getUser(u_username));
    }
    @PutMapping("/reset")
    Result<Boolean> resetPassword(String u_username,String u_password){
        if(staffService.resetPassword(u_username,u_password)==false){
            return Result.ErrorReSet(false);
        }
        return Result.successReSet(true);
    }
    @GetMapping("/getcustomer")
    IPage<StaffVo> getCustomer(@RequestParam(defaultValue = "1") Integer current,
                               @RequestParam(defaultValue = "10") Integer size){
        return staffService.getCustomer(current,size);
    }
    @PostMapping("/insertuser")
    Result<Boolean> insetUser(@RequestBody StaffEntity staffEntity){
        return Result.success(staffService.insetUser(staffEntity));
    }
    @DeleteMapping("/deletebyids/{ids}")
    Result<Boolean> deleteById(@PathVariable String ids){
        String[] idArray=ids.split(",");
        List<Long> idList=new ArrayList<>();
        for (String s:idArray){
            String trimmed = s.trim();
            idList.add(Long.parseLong(trimmed));
        }
        return Result.success(staffService.deleteById(idList));
    }
    @GetMapping("/getnumber")
    Result<List<String>> getNumber(){
        return Result.success(staffService.getNumber());
    }
    @PostMapping("/updateById")
    Result<Boolean> updateUser(@RequestBody StaffVo staffVo){
        return Result.success(staffService.updateUser(staffVo));
    }
    @GetMapping("/getallposition")
    Result<List<String>> getAllPosition(){
        return Result.success(staffService.getAllPosition());
    }
}
